<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>创建问卷</title>
    <link rel="stylesheet" href="/static/css/createpage.css">
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <style>
        .invalid::-webkit-input-placeholder {
            color: red;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="survey_card">
            <div class="survey_page">
                <div class="q_title">
                    <label>主题：</label> <br><input type="text" id="q_theme" placeholder="请输入问卷主题">
                </div>
                <div class="q_title">
                    <label>说明：</label> <br><input type="text" id="q_state" placeholder="请输入问卷说明">
                </div>
                <div class="question">
                    <table id="istable">
                    </table>
                    <div class="btn-css">
                        <input class=" btn btn-outline-primary" type="button" value="创建问题" onclick="addQuestion()">
                        <br>
                        <input class=" btn btn-outline-primary" type="button" value="保存" onclick="reset(istable);window.onbeforeunload = null;">
                    </div>
                </div>
            </div>

        </div>

        <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
        <script src="/static/js/jquery-3.6.0.min.js"></script>
        <script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
        <script>
            var unloadPageTip = function () { return "刷新页面有可能会丢失数据"; };
            window.onbeforeunload = unloadPageTip;
        </script>
        <script>
            function reset(istable) {
                if ($('#q_theme').val().length > 0) {
                    if ($('#q_state').val().length > 0) {
                        var arr = [];
                        var judge = "true";

                        for (var i = 1; i <= istable.rows.length; i++) {
                            var a = $(`#num_${i}`).val().trim();
                            if (a.length > 0) {
                                arr.push(a);
                                judge = "true";
                            } else {
                                alert("问卷问题不能为空，请重新输入")
                                judge = "false";
                                break;
                            }
                        }

                        if (judge === "true") {
                            if (arr.length === 0) {
                                alert('你还未创建问题')
                            } else {
                                axios({
                                    method: 'post',
                                    url: '/question',
                                    data: {
                                        content: arr,
                                        theme: $('#q_theme').val(),
                                        state: $('#q_state').val()
                                    }
                                }).then(function (response) {
                                    // axios工具下，服务器真正返回的数据在返回对象的data中
                                    let res = response.data;
                                    if (res.code === 200) {
                                        window.location.href = "/surveyAll"
                                    }else{
                                        alert(res.msg)
                                    }
                                }).catch((err) => {

                                });
                            }
                        } else if (judge === "false") {
                            alert("保存失败")
                        }

                    } else {
                        alert("问卷说明不能为空，请重新输入")
                    }
                } else {
                    alert("问卷主题不能为空，请重新输入")
                }
            }

        </script>


        <script "text/javascript">

            function addQuestion() {

                var re = '';
                var trlen = $('#istable').find('tr').length;
                re = `<tr>
                    <td style="width:30px;text-align: center;">
                        <label>${trlen + 1} . </label>
                    </td>
                    <td>
                        <input type="text" class="textbox"  id="num_${trlen + 1}" placeholder="请输入问题">
                    </td>
                    <td style="text-align: center;width:5vw">
                        <input class="remove_input" type="button" onclick="removeColumn(this)">
                    </td>
                </tr>`;

                $("#istable").append(re);
                sort();
            }

            function removeColumn(thisbutton) {
                $(thisbutton).parent().parent().remove();
                sort();
            }

            function sort() {
                let para = $("#istable tr").length;//获取tbody删除一行后tr的个数
                var arr = [];
                $("[id*='num_']").each(function (index, name) {
                    arr.push(index);
                    $('#istable input[type=text]').eq(index).attr('id', `num_${index + 1}`);
                });
                console.log(arr);
                for (let i = 0; i <= para; i++) { //循环遍历
                    //将遍历后的i给到每行的序号中
                    $('#istable').find('tr').eq(i).find('td:first').html(i + 1 + '.');

                }

            }


        </script>
</body>

</html>